package cn.com.gridinfo.sooc.core.my.msg.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jeremy.arad.utils.ImageGetterUtil;
import com.jeremy.arad.utils.StringUtils;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;

import java.util.List;

import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.core.course.discuss.activity.DiscussInfoActivity;
import cn.com.gridinfo.sooc.core.my.msg.bean.DiscussInfo;
import cn.com.gridinfo.sooc.core.my.msg.listener.MyItemLongClickListener;
import cn.com.gridinfo.sooc.utils.widget.textview.HtmlImageGetterUtil;

/**
 * Created by Michelle on 2016/1/6.
 */
public class DiscussMsgAdapter extends UltimateViewAdapter<DiscussMsgAdapter.ViewHolder> {
    private Context context;
    private List<DiscussInfo> discussList;
    private int deletePosition = -1;
    private MyItemLongClickListener mItemLongClickListener;
    public DiscussMsgAdapter(Context context, List<DiscussInfo> discussList) {
        this.context = context;
        this.discussList = discussList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        return null;
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public ViewHolder getViewHolder(View view) {
        return new ViewHolder(view, mItemLongClickListener, false);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) {
        //加载布局
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_msg_discuss_item, parent, false);
        return new ViewHolder(view, mItemLongClickListener, true);
    }

    @Override
    public int getAdapterItemCount() {
        return discussList != null ? discussList.size() : 0;
    }

    @Override
    public long generateHeaderId(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        if (position < getItemCount() && (customHeaderView != null ?
                position <= discussList.size() : position < discussList.size()) &&
                (customHeaderView != null ? position > 0 : true)) {
            final DiscussInfo discussInfo = discussList.get(position);
            holder.title.setText(discussInfo.getTitle());
            holder.content.setText(discussInfo.getHfnr());
            if (StringUtils.isNull(discussInfo.getSj())) {
                holder.time.setText("无");
            } else {
                if(position == 0){
                    holder.time.setVisibility(View.VISIBLE);
                    holder.time.setText(discussInfo.getSj());
                }else{
                    DiscussInfo  discussInfo2 = discussList.get(position-1);
                    if(discussInfo.getSj().equals(discussInfo2.getSj())){
                        holder.time.setVisibility(View.GONE);
                    }else {
                        holder.time.setVisibility(View.VISIBLE);
                        holder.time.setText(discussInfo.getSj());
                    }
                }
            }
            holder.rlToLook.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.putExtra("tlid", discussInfo.getTlid());
                    intent.setClass(context, DiscussInfoActivity.class);
                    context.startActivity(intent);
                }
            });
        }
    }

    public class ViewHolder extends UltimateRecyclerviewViewHolder implements View.OnLongClickListener {
        //通知名称、内容、时间
        TextView title, content, time;
        //查看详情
        RelativeLayout rlToLook;
        MyItemLongClickListener mLongClickListener;
        public ViewHolder(View itemView,MyItemLongClickListener longClickListener, boolean isItem) {
            super(itemView);
            this.mLongClickListener = longClickListener;
            if (isItem) {
                //控件布局
                title = (TextView) itemView.findViewById(R.id.my_msg_discuss_item_title);//课程名称
                content = (TextView) itemView.findViewById(R.id.my_msg_discuss_item_content);//课程名称
                time = (TextView) itemView.findViewById(R.id.my_msg_discuss_item_time);// 上次学习时间
                rlToLook = (RelativeLayout) itemView.findViewById(R.id.my_msg_discuss_item_to_look);
            }
        }
        @Override
        public boolean onLongClick(View v) {
            if(mLongClickListener != null){
                mLongClickListener.onItemLongClick(v, getPosition());
                deletePosition = getPosition();
            }
            return true;
        }
    }
    public void loadmore(List<DiscussInfo> list) {
        discussList.addAll(list);
        notifyDataSetChanged();

    }

    public void refresh(List<DiscussInfo> list) {
        clear(discussList);
        discussList.addAll(list);
        notifyDataSetChanged();
    }

    public void deleteSuccess() {
        discussList.remove(deletePosition);
        notifyDataSetChanged();
        deletePosition = -1;
    }
    public void setOnItemLongClickListener(MyItemLongClickListener listener){
        this.mItemLongClickListener = listener;
    }

    public List<DiscussInfo> getData(){
        return discussList;
    }


}
